﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ajayumi.Platform.Win.UserCtlPanel
{
    /// <summary>
    /// UserCtlPanelBase.cs
    /// Author   : 黄乙冬
    /// Date     : 2011-09-25
    /// </summary>
    [ToolboxItem(false)]
    public partial class UserCtlPanelBase : AsyncUserControl
    {
        #region Properties

        public Operation Op { get; set; }

        protected int RowIndex { get; set; }

        #endregion


        #region Delegates

        /// <summary>
        /// 添加前 的委托定义
        /// </summary>
        public Action BeforeAdd;

        /// <summary>
        /// 修改前 的委托定义
        /// </summary>
        public Action BeforeEdit;

        /// <summary>
        /// 删除前 的委托定义
        /// </summary>
        public Action BeforeDelete;

        /// <summary>
        /// 保存后 的委托定义
        /// </summary>
        public Action Saved;

        /// <summary>
        /// 取消后 的委托定义
        /// </summary>
        public Action Canceled;

        #endregion

        public UserCtlPanelBase()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加
        /// </summary>
        public virtual void Add()
        {
            this.RaiseBeforeAdd();
            this.Op = Operation.Create;
            this.EditorStatus();
        }

        /// <summary>
        /// 修改
        /// </summary>
        public virtual void Edit()
        {
            this.RaiseBeforeEdit();
            this.Op = Operation.Update;
            this.EditorStatus();
        }

        /// <summary>
        /// 删除
        /// </summary>
        public virtual void Delete()
        {
            this.RaiseBeforeDelete();
            this.Op = Operation.Delete;
        }

        /// <summary>
        /// 保存
        /// </summary>
        public virtual void Save()
        {
            this.Op = Operation.None;
            this.BrowserStatus();
            this.RaiseSaved();
        }

        /// <summary>
        /// 取消
        /// </summary>
        public virtual void Cancel()
        {
            this.Op = Operation.None;
            this.BrowserStatus();
            this.RaiseCanceled();
        }

        /// <summary>
        /// 导出
        /// </summary>
        public virtual void Export()
        { }

        /// <summary>
        /// 打印
        /// </summary>
        public virtual void Print()
        { }

        /// <summary>
        /// 下一条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T Next<T>() { return default(T); }

        /// <summary>
        /// 上一条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T Previous<T>() { return default(T); }

        /// <summary>
        /// 第一条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T First<T>() { return default(T); }

        /// <summary>
        /// 最后一条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T Last<T>() { return default(T); }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.ShowBase();
            this.BrowserStatus();
        }


        protected virtual void ShowBase()
        {
            return;
        }

        /// <summary>
        /// 浏览模式状态
        /// </summary>
        protected virtual void BrowserStatus()
        {
        }

        /// <summary>
        /// 编辑模式状态
        /// </summary>
        protected virtual void EditorStatus()
        {
        }


        /// <summary>
        /// 触发 添加前 的委托方法
        /// </summary>
        protected virtual void RaiseBeforeAdd()
        {
            if (this.BeforeAdd != null)
            { this.BeforeAdd(); }
        }

        /// <summary>
        /// 触发 修改前 的委托方法
        /// </summary>
        protected virtual void RaiseBeforeEdit()
        {
            if (this.BeforeEdit != null)
            { this.BeforeEdit(); }
        }

        /// <summary>
        /// 触发 删除前 的委托方法
        /// </summary>
        protected virtual void RaiseBeforeDelete()
        {
            if (this.BeforeDelete != null)
            { this.BeforeDelete(); }
        }

        /// <summary>
        /// 触发 保存后 的委托方法
        /// </summary>
        protected virtual void RaiseSaved()
        {
            if (this.Saved != null)
            { this.Saved(); }
        }

        /// <summary>
        /// 触发 取消后 的委托方法
        /// </summary>
        protected virtual void RaiseCanceled()
        {
            if (this.Canceled != null)
            { this.Canceled(); }
        }
    }
}
